﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.KindOfWaterUse;
using System.Linq.Expressions;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class KindOfWaterUseController : AdminController
    {
        [Import]
        private IKindOfWaterUseService KindOfWaterUseService{get;set;}

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
           var model=new KindOfWaterUseModel();
            return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = KindOfWaterUseService.KindOfWaterUses.Count(t => t.IsDeleted == false);
            var expr = BuildSearchCriteria();


            var filterResult = KindOfWaterUseService.KindOfWaterUses.Where(expr).Select(t => new KindOfWaterUseModel
            {
                Id = t.Id,
                Yong_SXZ=t.Yong_SXZ,
                OrderNo=t.OrderNo,
                Jie_TSZ=t.Jie_TSZ,
                Yong_SL=t.Yong_SL,
                Yong_SDJ=t.Yong_SDJ,
                Yong_SLA = t.Yong_SLA,
                Yong_SDJA=t.Yong_SDJA,
                Yong_SLB = t.Yong_SLB,
                Yong_SDJB=t.Yong_SDJB,
                Yong_SLC=t.Yong_SLC,
                Yong_SDJC=t.Yong_SDJC

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Yong_SXZ,
                            c.Jie_TSZ.ToString(),                          
                            c.Yong_SDJ.ToString(),
                            c.Yong_SL.ToString(),
                            c.Yong_SDJA.ToString(),
                            c.Yong_SLA.ToString(),
                            c.Yong_SDJB.ToString(),
                            c.Yong_SLB.ToString(),
                            c.Yong_SDJC.ToString(),
                            c.Yong_SLC.ToString(),                         
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new KindOfWaterUseModel();

            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(KindOfWaterUseModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = KindOfWaterUseService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new KindOfWaterUseModel();
            var entity = KindOfWaterUseService.KindOfWaterUses.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new KindOfWaterUseModel
                {
                    Yong_SXZ = entity.Yong_SXZ,
                    OrderNo = entity.OrderNo,
                    Jie_TSZ = entity.Jie_TSZ,
                    Yong_SL = entity.Yong_SL,
                    Yong_SDJ = entity.Yong_SDJ,
                    Yong_SLA = entity.Yong_SLA,
                    Yong_SDJA = entity.Yong_SDJA,
                    Yong_SLB = entity.Yong_SLB,
                    Yong_SDJB = entity.Yong_SDJB,
                    Yong_SLC = entity.Yong_SLC,
                    Yong_SDJC = entity.Yong_SDJC
                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(KindOfWaterUseModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = KindOfWaterUseService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = KindOfWaterUseService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<KindOfWaterUse, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<KindOfWaterUse> bulider = new DynamicLambda<KindOfWaterUse>();
            Expression<Func<KindOfWaterUse, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Yong_SXZ"]))
            {
                var data = Request["Yong_SXZ"].Trim();
                Expression<Func<KindOfWaterUse, Boolean>> tmp = t => t.Yong_SXZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<KindOfWaterUse, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }
}
